package org.catdroid.englishformusicians;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	ListView list = null;
	ArrayAdapter listAdapter = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// Creem les frases...
		int sentencesCount = 71;
		int idVO = -1;
		int idTrad = -1;
		String resource;
		Resources res = getResources();
		List sentences = new ArrayList<Sentence>();
		for (int i = 1; i <= sentencesCount; i++) {
			resource = "frase" + i;
			idVO = res.getIdentifier(resource + "_vo", "string", "org.catdroid.englishformusicians");
			idTrad = res.getIdentifier(resource, "string", "org.catdroid.englishformusicians");

			Sentence frase = new Sentence();
			frase.textoVO = getString(idVO);
			frase.textoTraducido = getString(idTrad);
			sentences.add(frase);
		}

		// Les posem a l'adapter...

		listAdapter = new AdapterAllScreen(this, sentences);
		list = (ListView) findViewById(R.id.list);
		list.setAdapter(listAdapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		Intent i = new Intent(this, AboutActivity.class);
		startActivity(i);
		return true;
	}

	public static String lastSentence;

	@Override
	protected void onResume() {
		super.onResume();

		listAdapter.notifyDataSetChanged();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == GoogleVoiceUtil.VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
			ArrayList<String> lastVoiceMatches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

			String cleanSentence = cleanSentenceString(lastSentence);

			boolean ok = false;
			for (String v : lastVoiceMatches) {

				if (v.toLowerCase().equals(cleanSentence)) {
					TestRepository.addChecked(this, cleanSentence);
					Log.i("VOICE", v + " - OK!");
					ok = true;
				} else {
					Log.i("VOICE", v + " - Nope!");
				}
			}

			if (!ok) {
				Toast.makeText(this, lastVoiceMatches.get(0), Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(this, "Cool!", Toast.LENGTH_SHORT).show();
			}
		}
	}

	public static String cleanSentenceString(String s) {
		return s.toLowerCase().replaceAll("   ", " ").replaceAll("  ", " ").replaceAll("!", "").replaceAll(",", "").replaceAll("\\?", "");
	}
}
